package cn.edu.mju.property.dao.impl;

import cn.edu.mju.property.dao.ComplaintDao;
import cn.edu.mju.property.entity.Complaint;
import cn.edu.mju.property.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ComplaintDaoImpl implements ComplaintDao {

    /*获取总页码*/
    @Override
    public int findTotalCount() {
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select count(*) from complaint";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            int num =  rs.getInt("count(*)");
            pstmt.close();
            conn.close();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Complaint> findByPage(int start, int rows) {
        Complaint complaint = null;
        List<Complaint> Complaintlist = new ArrayList<Complaint>();
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from complaint limit ? , ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,start);
            pstmt.setInt(2,rows);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                complaint = new Complaint();
                complaint.setId(rs.getInt("id"));
                complaint.setTitle(rs.getString("title"));
                complaint.setContent(rs.getString("content"));
                complaint.setTime_c(rs.getString("time_c"));
                complaint.setTime_h(rs.getString("time_h"));
                complaint.setUname(rs.getString("uname"));
                complaint.setStatus(rs.getInt("status"));
                Complaintlist.add(complaint);
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Complaintlist;
    }



    /* 删除投诉*/
    @Override
    public boolean delSelected(String id) {
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "delete from complaint where id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /*更新*/
    @Override
    public boolean upComplaint(Complaint complaint) {

        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = null;
            if(complaint.getStatus()==1){
                sql = "update complaint set title=?,conent=?,time_c=?,time_h=?,uname=?,status=? where id = ?";
            }else {
                sql = "update complaint set title=?,conent=?,time_c=?,time_h=NULL,uname=?,status=? where id = ?";
            }

            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, complaint.getTitle());
            pstmt.setString(2, complaint.getContent());
            pstmt.setString(3, complaint.getTime_c());
            pstmt.setString(4, complaint.getTime_h());
            pstmt.setString(5, complaint.getUname());
            pstmt.setInt(6, complaint.getStatus());
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public boolean upComplaintStatus(String id, String status) {

        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = null;
            if(status=="1"){
                sql = "update complaint set time_h=NULL,status=? where id = ?";
            }else {
                sql = "update complaint set time_h=now(),status=? where id = ?";
            }

            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, status);
            pstmt.setString(2, id);
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public Complaint findComplaint(String id) {
        Complaint complaint = null;
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from complaint where id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                complaint = new Complaint();
                complaint.setId(rs.getInt("id"));
                complaint.setTitle(rs.getString("title"));
                complaint.setContent(rs.getString("content"));
                complaint.setTime_c(rs.getString("time_c"));
                complaint.setTime_h(rs.getString("time_h"));
                complaint.setUname(rs.getString("uname"));

            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return complaint;
    }


}
